package com.ruoyi.driver.pay.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.IDMaker;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.driver.pay.service.IDriverPayService;
import com.ruoyi.management.basics.service.ITEmployeeMerService;
import com.ruoyi.management.basics.service.ITEmployeeService;
import com.ruoyi.management.basics.vo.DriverMerListVo;
import com.ruoyi.management.basics.vo.GetDriverDetailVo;
import com.ruoyi.management.statistical.driversign.ro.SaveDriverSignParam;
import com.ruoyi.management.statistical.driversign.service.ITDriverSignService;
import com.ruoyi.passenger.order.domain.TOrder;
import com.ruoyi.passenger.order.service.ITOrderService;
import com.ruoyi.passenger.pay.ro.PayParam;
import com.ruoyi.passenger.pay.util.PayUtils;
import com.ruoyi.passenger.pay.vo.GetDriverSignVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

/**
 * 驾驶员收款业务层处理
 *
 * @author ruoyi
 * @date 2021-12-01
 */
@Service
public class DriverPayServiceImpl implements IDriverPayService {


    @Autowired
    private ITEmployeeService employeeService;

    @Autowired
    private ITEmployeeMerService tEmployeeMerService;

    @Autowired
    private ITOrderService tOrderService;

    @Autowired
    private ITDriverSignService tDriverSignService;

    private static final String RETURNTYPE = "2";//调用银行接口返回收款二维码


    @Value("${pay.params.curCode}")
    private String curCode;

    @Value("${pay.params.txCode}")
    private String txCode;

    @Value("${pay.params.bankURL}")
    private String bankURL;

    @Value("${pay.params.payUrlPre}")
    private String payUrlPre;

    @Value("${pay.params.qupwd}")
    private String qupwd;

    @Value("${file.preUrl}")
    private String preUrl;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult getPayQrcode(PayParam param,LoginUser loginUser) {
        AjaxResult ajax = AjaxResult.success();
        if (null!= loginUser)
        {
            String username = loginUser.getUsername();
            GetDriverDetailVo vo =  employeeService.getEmployeeDetailByPhone(username);
            if (null != vo)
            {
                DriverMerListVo driverMer = tEmployeeMerService.getDriverMerByEmployeeId(vo.getId());
                if (null != driverMer)
                {
                    //获取第一次支付接口地址
                    ajax = PayUtils.getPayQrCode(driverMer,param.getPayment()+"",curCode,txCode,bankURL,RETURNTYPE,preUrl);

                    String payOrderId = String.valueOf(ajax.get("orderId"));
                    //创建订单
                    TOrder order =  new TOrder();
                    order.setOrderDate(new Date());//订单时间
                    order.setAmount(param.getPayment());//订单金额
                    order.setEmployeeId(driverMer.getEmployeeId());//驾驶员id
                    order.setEmployeeName(driverMer.getEmployeeName());//驾驶员姓名
                    order.setCompanyId(driverMer.getCompanyId());//企业 id
                    order.setCarNo(driverMer.getCarNo());//车牌号
                    order.setPayStatus("2");//支付状态，未支付
                    order.setIsSync(0);//未同步行程数据
                    order.setPayOrderId(payOrderId);//支付订单id
                    order.setPlatform(param.getPlatform());//支付平台
                    order.setPlatformId(param.getOpenid());//用户id
                    tOrderService.saveOrder(order);
                    return ajax;

                }else {
                    ajax.put("result","error");
                    ajax.put("msg","获取驾驶员商户信息失败，请选择其他支付方式");
                }
            }
        }

        return null;
    }

    @Override
    public AjaxResult getQrcodeForDriver(PayParam param, LoginUser loginUser) {
        AjaxResult ajax = AjaxResult.success();
        //获取当前驾驶员信息
        String username = loginUser.getUsername();
        GetDriverDetailVo vo =  employeeService.getEmployeeDetailByPhone(username);
        //获取当前车辆
        GetDriverSignVO signVO = tDriverSignService.getDriverSignVoByDriver(vo.getIdcard());
        if (null != signVO){
            String url = payUrlPre + "?carNo=" + signVO.getCarNo()+"&payment=" + param.getPayment();
            String carNo = signVO.getCarNo();
            GetDriverSignVO signVO1 = tDriverSignService.getDriverSignVO(carNo);
            if (null != signVO1 && !signVO1.getEmployeeId().equals(signVO.getEmployeeId()))
            {
                ajax = AjaxResult.error();
                ajax.put("msg","系统检测到当前该车驾驶员为:《"+signVO1.getEmployeeName()+"》,与当前支付对象不符，请先进行打卡操作");
                ajax.put("result","error01");
                ajax.put("url",url);
            }else {
                ajax.put("url",url);
                ajax.put("result","success");
            }
        }else {
            ajax = AjaxResult.error();
            ajax.put("msg","未获取到驾驶员驾驶车辆信息，请选择其他支付方式");
            ajax.put("result","error02");
        }

        return ajax;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int driverSign(LoginUser loginUser,PayParam param) {
        String username = loginUser.getUsername();
        //驾驶员信息
        GetDriverDetailVo vo =  employeeService.getEmployeeDetailByPhone(username);
        //当前车辆信息
        String carNo = param.getCarNo();
        if(vo == null){
            throw new ServiceException("获取当前驾驶员失败，请稍后再试");
        }
        if(StringUtils.isEmpty(carNo)){
            throw new ServiceException("车牌号不能为空");
        }
        //保存打卡信息
        SaveDriverSignParam pa = new SaveDriverSignParam();
        pa.setId(IDMaker.uuid19());
        pa.setCarNo(carNo);
        pa.setIdNo(vo.getIdcard());
        return tDriverSignService.save(pa);
    }

    @Override
    public AjaxResult toDriverSign(LoginUser loginUser) {
        String username = loginUser.getUsername();

        //驾驶员信息
        GetDriverDetailVo vo =  employeeService.getEmployeeDetailByPhone(username);
        //当前车辆信息
        GetDriverSignVO signVO = tDriverSignService.getDriverSignVoByDriver(vo.getIdcard());
        String carNo = "";
        if (null!= signVO && null != signVO.getCarNo() &&!"".equals(signVO.getCarNo()))
        {
            carNo = signVO.getCarNo();
        }
        return AjaxResult.success(carNo);
    }

}
